package com.huawei.controller;

import com.alibaba.fastjson.JSON;
import com.huawei.entity.Jinebiandong;

import com.huawei.service.JinebiandongService;

import com.huawei.util.Result;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JinebiandongController
 * @Description TODO
 * @Author 快乐的大胖
 * @Date 2021/11/29 20:26
 */

@Controller
@RequestMapping("Jinebiandong")
public class JinebiandongController {


    @Resource
    JinebiandongService jinebiandongImpl;


    //查询
    @RequestMapping(value = "/chaxun", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String selectAll(
            @RequestParam(value = "user_id", required = false) Integer user_id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam("limit") Integer limit,
            @RequestParam("page") Integer page
    ){
        Map<String, Object> pars = new HashMap<String, Object>();
        pars.put("user_id",user_id);
        pars.put("name",name);
        pars.put("offset", (page - 1) * limit);
        pars.put("limit", limit);



        List<Jinebiandong> list = jinebiandongImpl.selectAll(pars);
        int count = jinebiandongImpl.selectCount(pars);
        Result result = new Result(0, "查询成功", count, list);
        return JSON.toJSONString(result);
    }

    //修改
    @RequestMapping(value = "update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String update(
      @RequestBody    Jinebiandong jinebiandong
    ) {
        int i = jinebiandongImpl.update(jinebiandong);
        if (i == 1) {
            return "修改成功";
        } else {
            return "修改失败";

        }


    }

    //添加
    @RequestMapping(value = "add", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String add(
      @RequestBody Jinebiandong jinebiandong
    ) {
        int i = jinebiandongImpl.add(jinebiandong);
        if (i == 1) {
            return "添加成功";
        } else {
            return "添加失败";

        }


    }

    //删除
    @RequestMapping(value = "detele", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String detele(
            @RequestParam ("jilu_id")  Integer jilu_id
    ){


        int i = jinebiandongImpl.delete(jilu_id);

        if (i == 1) {
            return "删除成功";
        } else {
            return "删除失败";

        }

    }




}
